package cn.caplike.demo.nio._2021.buffer;

import java.nio.IntBuffer;
import java.security.SecureRandom;

/**
 * @author LiKe
 * @version 1.0.0
 * @date 2021-12-21 13:09
 */
public class NioQuickstart {

    public static void main(String[] args) {
        IntBuffer buffer = IntBuffer.allocate(10);

        for (int i = 0; i < 5; ++i) {
            int randomNumber = new SecureRandom().nextInt(20);
            buffer.put(randomNumber);
        }

        System.out.println("before flip limit: " + buffer.limit());
        buffer.flip(); // 翻转: 写 -> 读
        System.out.println("after flip limit: " + buffer.limit());

        while (buffer.hasRemaining()) {
            System.out.println("position: " + buffer.position());
            System.out.println("limit: " + buffer.limit());
            System.out.println("capacity: " + buffer.capacity());

            System.out.println(buffer.get());
        }
    }
}
